package com.xoa.service.financialManage.charge.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xoa.dao.financialManage.charge.ChargeListMapper;
import com.xoa.dao.financialManage.charge.ChargeSettingMapper;
import com.xoa.dao.financialManage.charge.ChargeSettingTypeMapper;
import com.xoa.dao.financialManage.charge.ChargeTypeMapper;
import com.xoa.model.financialManage.charge.*;
import com.xoa.model.users.Users;
import com.xoa.service.financialManage.charge.ChargeListService;
import com.xoa.util.CookiesUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 费用信息 服务实现类
 * </p>
 *
 * @author liujian
 * @since 2020-04-23
 */
@Service
public class ChargeListServiceImpl extends ServiceImpl<ChargeListMapper, ChargeList> implements ChargeListService {

    @Resource
    ChargeListMapper chargeListMapper;

    @Resource
    ChargeTypeMapper chargeTypeMapper;

    @Resource
    ChargeSettingMapper chargeSettingMapper;

    @Resource
    ChargeSettingTypeMapper chargeSettingTypeMapper;

    @Override
    public ToJson<ChargeList> save(HttpServletRequest request, ChargeList chargeList) {
        ToJson<ChargeList> toJson = new ToJson<>(1, "err");
        Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
        try {
            int month = new Date().getMonth();
            chargeList.setCreator(users.getUserId());
            chargeList.setCreateTime(new Date());
            chargeList.setCreatorQuarter(String.valueOf(((month) / 3) + 1));
            int i = chargeListMapper.insertSelective(chargeList);
            if (i > 0) {
                toJson.setFlag(0);
                toJson.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }

        return toJson;
    }

    @Override
    public ToJson<ChargeList> deleteById(Integer chargeListId) {
        ToJson<ChargeList> toJson = new ToJson<>(1, "err");
        try {
            int i = chargeListMapper.deleteByPrimaryKey(chargeListId);
            if (i > 0) {
                toJson.setFlag(0);
                toJson.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }

        return toJson;
    }

    @Override
    public ToJson<ChargeList> update(ChargeList ChargeList) {
        ToJson<ChargeList> toJson = new ToJson<>(1, "err");
        try {
            int i = chargeListMapper.updateByPrimaryKeySelective(ChargeList);
            if (i > 0) {
                toJson.setFlag(0);
                toJson.setMsg("true");
            }
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }

        return toJson;
    }

    @Override
    public ToJson<ChargeList> selectById(Integer chargeListId) {
        ToJson<ChargeList> toJson = new ToJson<>(1, "err");
        try {
            ChargeList chargeList = chargeListMapper.selectByPrimaryKey(chargeListId);
            if (chargeList != null) {
                toJson.setFlag(0);
                toJson.setMsg("true");
                toJson.setObject(chargeList);
            }
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }

        return toJson;
    }

    @Override
    public ToJson<ChargeList> conChargeList(HttpServletRequest request, Integer deptId, String key, String filter, Integer typeId, String undertaker, Integer year, Integer month, Integer day) {
        ToJson<ChargeList> toJson = new ToJson<>(1, "err");
        Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
        try {
            String typeIds = "";
            if (typeId != null) {
                List<ChargeType> chargeTypeList = chargeTypeMapper.selectByParent(typeId);
                if (chargeTypeList != null) {
                    for (ChargeType chargeType : chargeTypeList) {
                        typeIds += chargeType.getChargeTypeId() + ",";
                    }
                    if (typeIds.endsWith(",")) {
                        typeIds = typeIds.substring(0, typeIds.length() - 1);
                    }
                }
            }

            List<ChargeList> chargeLists = chargeListMapper.conChargeList(deptId != null ? null : users.getUserId(), deptId, key, filter, typeIds, undertaker, year, month, day);
            if (chargeLists != null) {
                toJson.setFlag(0);
                toJson.setMsg("true");
                toJson.setObj(chargeLists);
            }
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }

        return toJson;
    }

    @Override
    public ToJson<ChargeType> findMyChargeList(HttpServletRequest request, Integer deptId, String filter, Integer year, Integer month) {
        ToJson<ChargeType> toJson = new ToJson<>(1, "err");
        Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
        try {
            BigDecimal bigDe = new BigDecimal(0.00);
            String userId = deptId != null ? null : users.getUserId();
            List<ChargeList> myChargeList = chargeListMapper.findMyChargeList(userId, deptId, filter, year, month);
            if (myChargeList != null) {
                Map<Integer, List<ChargeList>> integerListMap = new HashMap<>();
                Map<String, BigDecimal> lineMap = new HashMap();
                for (ChargeList chargeList : myChargeList) {
                    List<ChargeList> chargeLists = integerListMap.get(chargeList.getChargeTypeId());
                    if (chargeLists != null) {
                        chargeLists.add(chargeList);
                    } else {
                        List<ChargeList> chargeLists1 = new ArrayList<>();
                        chargeLists1.add(chargeList);
                        integerListMap.put(chargeList.getChargeTypeId(), chargeLists1);
                    }
                    String key = (chargeList.getPaymentDate().getMonth() + 1) +"";
                    switch (filter) {
                        case "quarter":
                            key = chargeList.getCreatorQuarter();
                            break;
                        case "month":
                            key = chargeList.getPaymentDate().getDate()+"";
                            break;
                    }
                    BigDecimal bigDecimal1 = lineMap.get(key);
                    if (bigDecimal1 != null) {
                        lineMap.put(key, bigDecimal1.add(chargeList.getChargeCost()));
                    } else {
                        lineMap.put(key, chargeList.getChargeCost());
                    }
                    bigDe = bigDe.add(chargeList.getChargeCost());
                }

                ChargeTypeExample chargeTypeExample = new ChargeTypeExample();
                chargeTypeExample.createCriteria().andChargeTypeParentEqualTo(0);
                List<ChargeType> chargeTypes = chargeTypeMapper.selectByExample(chargeTypeExample);
                if (chargeTypes != null) {
                    chargeTypes.stream().forEach(chargeType -> {
                        BigDecimal bigDecParent = new BigDecimal(0.00);
                        List<ChargeType> typeList = chargeTypeMapper.selectByParent(chargeType.getChargeTypeId());
                        for (ChargeType chargeType1 : typeList) {
                            List<ChargeList> chargeLists = integerListMap.get(chargeType1.getChargeTypeId());
                            Map<String, BigDecimal> map = new HashMap();
                            if (chargeLists != null) {
                                int sum = 12;
                                BigDecimal bigDecimal = new BigDecimal(0.00);
                                chargeType1.setBigDecimalMap(map);
                                for (ChargeList chargeList : chargeLists) {
                                    bigDecimal = bigDecimal.add(chargeList.getChargeCost());
                                    Integer dateTime = chargeList.getPaymentDate().getMonth() + 1;
                                    switch (filter) {
                                        case "quarter":
                                            dateTime = Integer.valueOf(chargeList.getCreatorQuarter());
                                            sum = 4;
                                            break;
                                        case "month":
                                            Calendar cal=Calendar.getInstance();
                                            cal.setTime(chargeList.getPaymentDate());
                                            sum =cal.getActualMaximum(Calendar.DATE);
                                            dateTime = chargeList.getPaymentDate().getDate();
                                            break;
                                    }
                                    map.put(dateTime+"", chargeList.getChargeCost());
                                }
                                //补充数据
                                for(int i = 1 ; i<= sum ;i++){
                                    BigDecimal bigDecimal1 = map.get(i+"");
                                    if(bigDecimal1 == null ){
                                        map.put(i+"",new BigDecimal(0.00));
                                    }
                                }
                                chargeType1.setChargeCostSum(bigDecimal);
                                bigDecParent = bigDecParent.add(bigDecimal);
                            }
                        }
                        chargeType.setChildList(typeList);
                        chargeType.setChargeCostSum(bigDecParent);
                    });

                }
                toJson.setFlag(0);
                toJson.setMsg("true");
                toJson.setObj(chargeTypes);
                toJson.setObject(lineMap);
                toJson.setObj1(bigDe);
            }
        } catch (Exception e) {
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }

        return toJson;
    }

    @Override
    public ToJson<ChargeType> chargeStatistical(HttpServletRequest request, String type,Integer page,Integer limit) {
        ToJson toJson=new ToJson(1,"暂无数据");
        Cookie redisSessionId = CookiesUtil.getCookieByName(request, "redisSessionId");
        Users users = SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users(), redisSessionId);
        Map map=new HashMap();
        try {
            PageParams pageParams=new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(limit);
            pageParams.setUseFlag(true);
            map.put("pageParams",pageParams);
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            //获取当前登陆人的，部门，角色，人员,目前权限表只有用户所以根据用户查询就可以 只会有一条记录
            Integer userPriv = users.getUserPriv();
            Integer deptId = users.getDeptId();
            String userId = users.getUserId();
            Map<String,Object> permission=chargeListMapper.findpermissionByusers(userId);
            if (null != permission && !permission.isEmpty()){
                //权限范围，2-部门，3-角色，4-用户。这里的判断是否是部门和用户（角色也属于用户）,系统没有公司的概念所以去掉公司选项
                String setType=(String) permission.get("SET_TYPE");
                //管理范围
                String managerArea=(String) permission.get("MANAGER_AREA");
                //是否包含子部门1是2否
                String hasChildren=(String) permission.get("HAS_CHILDREN");
                //返回结果集
                List<Map<String,Object>> reslist=new ArrayList();

                //判断查找的类型2部门，3用户
                if (type.equals("2")){
                    //权限范围
                    if (setType.equals("2")){
                        //部门。暂时不考虑分支机构
                        //判断是否包含子部门
                        if (hasChildren.equals("1")){
                            map.put("managerArea",managerArea);
                            reslist=chargeListMapper.deptHasChildren(map);
                        }else if (hasChildren.equals("2")){
                            String[] split = managerArea.split(",");
                            map.put("depts",split);
                            reslist=chargeListMapper.deptChargeList(map);
                        }
                    }else if (setType.equals("3") || setType.equals("4")){
                        //角色和用户
                        Set<String> set=new HashSet();

                        //查找 某个角色下或某个userId的用户 ，并去重
                        String[] split = managerArea.split(",");
                        map.put( setType.equals("3") ? "userPrivs" : "userIds",split);
                        List<Map<String, Object>> usermaps = chargeListMapper.usersChargeList(map);
                        for (Map<String,Object> umap:usermaps) {
                            set.add(umap.get("DEPT_ID").toString());
                        }

                        //查找部门
                        map.put("depts",set);
                        reslist=chargeListMapper.deptChargeList(map);
                    }
                }else if (type.equals("3")){
                    //权限范围
                    if (setType.equals("2")){
                        //部门。暂时不考虑分支机构
                        List deptlist=new ArrayList();
                        //判断是否包含子部门
                        if (hasChildren.equals("1")){
                            map.put("managerArea",managerArea);
                            deptlist=chargeListMapper.deptHasChildren(map);
                        }else if (hasChildren.equals("2")){
                            String[] split = managerArea.split(",");
                            map.put("depts",split);
                            deptlist=chargeListMapper.deptChargeList(map);
                        }

                        //查找部门下的用户
                        map.put("deptIds",deptlist);
                        reslist = chargeListMapper.usersChargeList(map);
                    }else if (setType.equals("3") || setType.equals("4")){
                        //角色和用户

                        //查找 某个角色下或某个userId的用户 ，并去重
                        String[] split = managerArea.split(",");
                        map.put( setType.equals("3") ? "userPrivs" : "userIds",split);
                        reslist = chargeListMapper.usersChargeList(map);
                    }
                }

                //统计费用
                for (Map<String,Object> objectMap:reslist) {
                    Map map1=new HashMap();

                    if (type.equals("2")) {
                        map1.put("setType","2");
                        map1.put("deptId",objectMap.get("DEPT_ID").toString());
                    }else if (type.equals("3")) {
                        map1.put("setType","3");
                        map1.put("userId",objectMap.get("USER_ID").toString());
                    }
                    //查找预警设置 预警方式  预警金额	预警期 总计报销
                    List<ChargeSetting> setingAll = chargeSettingMapper.findSetingAll(map1);
                    if (!setingAll.isEmpty()){
                        ChargeSetting chargeSetting = setingAll.get(0);
                        //主键
                        objectMap.put("settingId",chargeSetting.getSettingId());
                        //预警方式 （1年，2季度，3月，4自定义周期）
                        String alertMethod = chargeSetting.getAlertMethod();
                        objectMap.put("alertMethod",alertMethod);
                        //预警金额
                        String alertType = chargeSetting.getAlertType();
                        objectMap.put("alertType",alertType);
                        if (alertType.equals("1")){
                            objectMap.put("alertValue",chargeSetting.getAlertValue());
                        }
                        //预警期
                        Date createdTime = chargeSetting.getCreatedTime();
                        if (alertMethod.equals("1")){
                            //年
                            DateFormat df=new SimpleDateFormat("yyyy");
                            String formatdate=df.format(createdTime);
                            objectMap.put("createdTime",formatdate);
                        }else if (alertMethod.equals("2")){
                            //季
                            DateFormat nian=new SimpleDateFormat("yyyy");
                            String niandate=nian.format(createdTime);

                            DateFormat df=new SimpleDateFormat("MM");
                            String formatdate=df.format(createdTime);
                            int m=Integer.parseInt(formatdate);
                            String quarter="";
                            if (m >= 1 && m <= 3) {
                                quarter = "一月-三月（第一季度）";
                                String monthStart = com.xoa.util.DateFormat.getMonthStart(Integer.parseInt(niandate), 1);
                                String monthEnd = com.xoa.util.DateFormat.getMonthEnd(Integer.parseInt(niandate), 3);
                                objectMap.put("monthStart",monthStart);
                                objectMap.put("monthEnd",monthEnd);
                            }
                            if (m >= 4 && m <= 6) {
                                quarter = "四月-六月（第二季度）";
                                String monthStart = com.xoa.util.DateFormat.getMonthStart(Integer.parseInt(niandate), 4);
                                String monthEnd = com.xoa.util.DateFormat.getMonthEnd(Integer.parseInt(niandate), 6);
                                objectMap.put("monthStart",monthStart);
                                objectMap.put("monthEnd",monthEnd);
                            }
                            if (m >= 7 && m <= 9) {
                                quarter = "七月-九月（第三季度）";
                                String monthStart = com.xoa.util.DateFormat.getMonthStart(Integer.parseInt(niandate), 7);
                                String monthEnd = com.xoa.util.DateFormat.getMonthEnd(Integer.parseInt(niandate), 9);
                                objectMap.put("monthStart",monthStart);
                                objectMap.put("monthEnd",monthEnd);
                            }
                            if (m >= 10 && m <= 12) {
                                quarter = "十月-十二月（第四季度）";
                                String monthStart = com.xoa.util.DateFormat.getMonthStart(Integer.parseInt(niandate), 10);
                                String monthEnd = com.xoa.util.DateFormat.getMonthEnd(Integer.parseInt(niandate), 12);
                                objectMap.put("monthStart",monthStart);
                                objectMap.put("monthEnd",monthEnd);
                            }
                            objectMap.put("createdTime",quarter);
                        }else if (alertMethod.equals("3")){
                            //月
                            DateFormat df=new SimpleDateFormat("yyyy-MM");
                            String formatdate=df.format(createdTime);
                            objectMap.put("createdTime",formatdate);
                        }else if(alertMethod.equals("4")){
                            //周期
                            objectMap.put("createdTime",chargeSetting.getAlertDataStart()+"/"+chargeSetting.getAlertDataEnd());
                        }

                        map1.clear();
                        Map map2=new HashMap();

                        //总计报销 查询charge_list表根据条件 （费用承担者2部门）
                        if (type.equals("2")) {
                            //条件一
                            String deptId1= objectMap.get("DEPT_ID").toString();
                            String  []deptIds={deptId1};
                            map2.put("deptIds",deptIds);

                            //条件二
                            map2.put("groupConcat","USER_ID");
                            List<Map<String, Object>> maps = chargeListMapper.usersChargeList(map2);
                            String groupConcat=(String)maps.get(0).get("groupConcat");
                            if (!StringUtils.checkNull(groupConcat)){
                                String[] split = groupConcat.split(",");
                                map1.put("userIds",split);
                            }

                            //条件三
                            map1.put("sum","CHARGE_COST");

                            //条件四
                            map1.put("chargeUndertaker","2");

                            List<ChargeList> findChargeList= chargeListMapper.findChargeList(map1);
                            objectMap.put("total", !findChargeList.isEmpty() ? findChargeList.get(0).getSum() : 0 );

                        }else if (type.equals("3")){
                            //条件一
                            map1.put("userId",objectMap.get("USER_ID"));

                            //条件二
                            map1.put("sum","CHARGE_COST");

                            //条件三
                            map1.put("chargeUndertaker","1");

                            List<ChargeList> findChargeList= chargeListMapper.findChargeList(map1);
                            objectMap.put("total", !findChargeList.isEmpty() ? findChargeList.get(0).getSum() : 0 );
                        }

                        //已报销
                        //条件一
                        if (type.equals("2")) {
                            map1.put("chargeUndertaker","2");

                            String deptId1= objectMap.get("DEPT_ID").toString();
                            String  []deptIds={deptId1};
                            map2.put("deptIds",deptIds);

                            map1.put("groupConcat","USER_ID");
                            List<Map<String, Object>> maps = chargeListMapper.usersChargeList(map2);
                            String groupConcat=(String)maps.get(0).get("groupConcat");
                            if (!StringUtils.checkNull(groupConcat)){
                                String[] split = groupConcat.split(",");
                                map1.put("userIds",split);
                            }

                        }else if (type.equals("3")) {
                            map1.put("chargeUndertaker","1");
                            map1.put("userId",objectMap.get("USER_ID"));
                        }

                        //条件二
                        if (alertMethod.equals("1")){
                            //年
                            DateFormat df=new SimpleDateFormat("yyyy");
                            String formatdate=df.format(createdTime);
                            map1.put("paymentDateSta",formatdate);
                            map1.put("paymentDateEnd",formatdate);
                        }else if (alertMethod.equals("2")){
                            //季
                            DateFormat df=new SimpleDateFormat("MM");
                            String formatdate=df.format(createdTime);
                            int m=Integer.parseInt(formatdate);
                            String quarter1="";
                            String quarter2="";
                            Calendar date = Calendar.getInstance();
                            String year = String.valueOf(date.get(Calendar.YEAR));
                            if (m >= 1 && m <= 3) {
                                quarter1= year+"-01";
                                quarter2= year+"-03";
                            }
                            if (m >= 4 && m <= 6) {
                                quarter1= year+"-04";
                                quarter2= year+"-06";
                            }
                            if (m >= 7 && m <= 9) {
                                quarter1= year+"-07";
                                quarter2= year+"-09";
                            }
                            if (m >= 10 && m <= 12) {
                                quarter1= year+"-10";
                                quarter2= year+"-12";
                            }
                            map1.put("paymentDateSta",quarter1);
                            map1.put("paymentDateEnd",quarter2);
                        }else if (alertMethod.equals("3")){
                            //月
                            DateFormat df=new SimpleDateFormat("yyyy-MM");
                            String formatdate=df.format(createdTime);
                            map1.put("paymentDateSta",formatdate);
                            map1.put("paymentDateEnd",formatdate);
                        }else if(alertMethod.equals("4")){
                            //周期
                            map1.put("paymentDateSta",chargeSetting.getAlertDataStart());
                            map1.put("paymentDateEnd",chargeSetting.getAlertDataEnd());
                        }

                        //条件三
                        map1.put("sum","CHARGE_COST");

                        //已报销 查询charge_list表根据条件 （费用承担者1个人，2部门）+（报销日期）
                        List<ChargeList> findChargeList= chargeListMapper.findChargeList(map1);
                        objectMap.put("Reimbursed", !findChargeList.isEmpty() ? findChargeList.get(0).getSum() : 0 );


                        //剩余报销= 总计报销-已报销
                        BigDecimal total=(BigDecimal) objectMap.get("total");
                        BigDecimal Reimbursed=(BigDecimal) objectMap.get("Reimbursed");
                        BigDecimal subtract = total.subtract(Reimbursed);
                        objectMap.put("remaining",subtract);
                    }
                }

                toJson.setObj(reslist);
                PageParams pageParams=(PageParams)map.get("pageParams");
                toJson.setTotleNum(pageParams.getTotal());
                toJson.setMsg("ok");
            }
            toJson.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }

    /**
     * 查看所有细明
     * @param type(2.部门，3用户)
     * @param id(如果是部门就传部门ID，如果是用户就传用户userID)
     * @param request
     * @param paymentDateEnd
     * @param paymentDateEnd
     * @param page
     * @param limit
     * @return
     */
    @Override
    public ToJson<ChargeType> chargeDetail(HttpServletRequest request,
                                           String type,
                                           String id,
                                           String paymentDateSta,
                                           String paymentDateEnd,
                                           String settingId,
                                           Integer page,
                                           Integer limit) {
        ToJson toJson=new ToJson(1,"暂无数据");
        Map  map=new HashMap();
        try {
            PageParams pageParams=new PageParams();
            pageParams.setPage(page);
            pageParams.setPageSize(limit);
            pageParams.setUseFlag(true);
            map.put("pageParams",pageParams);
        }catch (Exception e){
            e.printStackTrace();
        }
        try {
            if (type.equals("2")){
                Map map1=new HashMap();
                map.put("chargeUndertaker","2");

                String[] deptIds={id};
                map1.put("deptIds",deptIds);
                map1.put("groupConcat","USER_ID");
                List<Map<String,Object>> reslist = chargeListMapper.usersChargeList(map1);
                if (!reslist.isEmpty()){
                    String groupConcat=reslist.get(0).get("groupConcat").toString();
                    String[] split = groupConcat.split(",");
                    map.put("userIds",split);
                }
            }else if (type.equals("3")){
                map.put("chargeUndertaker","1");
                map.put("userId",id);
            }
            //时间
            map.put("paymentDateSta",paymentDateSta);
            map.put("paymentDateEnd",paymentDateEnd);
            List<ChargeList> findChargeList= chargeListMapper.findChargeList(map);
            for (ChargeList chargeList:findChargeList) {
                ChargeType chargeType = chargeTypeMapper.selectByPrimaryKey(chargeList.getChargeTypeParentid());
                chargeList.setChargeTypeParentName(chargeType!=null && !StringUtils.checkNull(chargeType.getChargeTypeName()) ? chargeType.getChargeTypeName() : "");
                ChargeType chargeType2 = chargeTypeMapper.selectByPrimaryKey(chargeList.getChargeTypeId());
                chargeList.setChargeTypeName(chargeType2!=null && !StringUtils.checkNull(chargeType2.getChargeTypeName()) ? chargeType2.getChargeTypeName() : "");

                if (!StringUtils.checkNull(settingId) && null != chargeList.getChargeTypeId()){
                    ChargeSettingTypeExample chargeSettingTypeExample=new ChargeSettingTypeExample();
                    ChargeSettingTypeExample.Criteria criteria = chargeSettingTypeExample.createCriteria();
                    criteria.andSettingIdEqualTo(Integer.parseInt(settingId));
                    criteria.andChargeTypeIdEqualTo(chargeList.getChargeTypeId());
                    List<ChargeSettingType> chargeSettingTypes = chargeSettingTypeMapper.selectByExample(chargeSettingTypeExample);
                    if (!chargeSettingTypes.isEmpty()){
                        BigDecimal chargeTypeValue = chargeSettingTypes.get(0).getChargeTypeValue();
                        chargeList.setChargeTypeValue(chargeTypeValue);
                        chargeList.setRemaining(chargeTypeValue.subtract(chargeList.getChargeCost()));
                    }
                }else{
                    chargeList.setChargeTypeValue(new BigDecimal(0));
                    chargeList.setRemaining(chargeList.getChargeCost());
                }

            }
            if (!findChargeList.isEmpty()){
                toJson.setObj(findChargeList);
                PageParams pageParams=(PageParams)map.get("pageParams");
                toJson.setTotleNum(pageParams.getTotal());
                toJson.setMsg("Ok");
                toJson.setFlag(0);
            }
        }catch (Exception e){
            e.printStackTrace();
            toJson.setMsg(e.getMessage());
        }
        return toJson;
    }
}
